const http = require('http')
const mongoose = require('mongoose')
const fs = require('fs')
const app = http.createServer()
// 连接数据库
mongoose.connect('mongodb://localhost/xia')
    .then(() => {
        console.log('数据库连接成功...')
        //建表
        const UserInfo = mongoose.model('UserInfo', new mongoose.Schema({
            name: {
                type: String,
                minlength: [2, '用户名不能小于2个字符'],
                maxlength: 20,
            },
            age: {
                type: Number,
                min: 2,
                max: [50, '年龄不能超过50岁']
            },
            hobbies: [String],
            email: String,


        }))
        // 查询数据
        // UserInfo.find({}).then(res => {
        //     console.log(res)
        // })
        // .catch(err => {
        //     console.log(err)
        // })
        // 插入数据
        // User.create({
        //     name: '周一帆',
        //     age: 21,
        //     password: 'asdf',
        //     email: '784@qq.com',
        //     hobbies: ['看电视', '八卦']
        // })

        app.on('request', (req, res) => {
            
            if (req.url != '/favicon.ico') {
                req.url = req.url == '/' ? '/list' : req.url


                const method = req.method

                if (method == 'GET') {
                    const { searchParams, pathname } =
                        new URL(req.url, 'http://127.0.0.1:3000')
                    res.writeHead(200, { 'content-type': 'text/html;charset=UTF8' })
                    // 路径为/list 查询所有信息
                    if (req.url == '/list') {
                        //查询所有数据
                        UserInfo.find({}).then(result => {
                            let userhtml = ''
                            userhtml += `
                                <!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <style>
        a {
            text-decoration: none;
            
        }
        #userlist {
            width: 600px;
            margin: 100px auto;
        }
        table {
            border: 1px solid;
            border-collapse: collapse;
            width: 100%;
        }
        table tr th,
        table tr td{
            border: 1px solid;
        }
    </style>
</head>
<body>
    <div id="userlist">
        <a href="/adduser">添加用户</a>
        <div>
            <table>
                <thead>
                    <tr>
                        <th>用户名</th>
                        <th>年龄</th>
                        <th>爱好</th>
                        <th>邮箱</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                                `
                            // 数据部分
                            for (let item of result) {
                                userhtml += `
                                    <tr>
                                    <td>${item.name}</td>
                                    <td>${item.age}</td>
                                    <td>${item.hobbies}</td>
                                    <td>${item.email}</td>
                                    <td>
                                        <a href="deleteuser">删除</a>   <a href="modify">修改</a>
                                    </td>
                                </tr>
                                    `
                            }


                            userhtml += `
                                </tbody>
                                </table>
                            </div>
                        </div>
                    </body>
                    </html>
                                `
                            console.log('发送网页')
                            res.end(userhtml)
                        })
                            .catch(err => {
                                console.log(err)
                            })

                    }else if(req.url == '/adduser') {
                        fs.readFile('./adduser.html', (err, data) => {
                            if(err) {
                                console.log('读取出错文件', err)
                                return 
                            }
                            res.end(data)
                        })
                    }

                } else if (method == 'POST') {
                    res.end('hello')
                }



                
            }

        })

    })
    .catch(err => {
        console.log(err)
    })


app.listen(3000, () => {
    console.log('start Server at http://127.0.0.1:3000')
})
